// This file is part of OpenMeca, an easy software to do mechanical simulation.
//
// Author(s)    :  - Damien ANDRE  <openmeca@gmail.com>
//
// Copyright (C) 2012 Damien ANDRE
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.


#include "OpenMeca/Item/LinkList.hpp"
#include "OpenMeca/Gui/MainWindow.hpp"
#include "OpenMeca/Core/Drawable.hpp"
#include "OpenMeca/Core/System.hpp"

#include "OpenMeca/Core/Drawable.hpp" 
#include "OpenMeca/Item/PartUserPoint.hpp" 

namespace OpenMeca
{  
  namespace Item
  {
    const std::string 
    Revolute::GetStrType()
    {
      return "Revolute";
    }

    template<> 
    void 
    Revolute::UpdatePartIcon<1>(QIcon& icon, QColor color)
    {
      Revolute::DrawIcon(icon, color, QColor(255,255,255,0));
    }

    template<> 
    void 
    Revolute::UpdatePartIcon<2>(QIcon& icon, QColor color)
    {
      Revolute::DrawIcon(icon, QColor(255,255,255,0), color);
    }



    template<> 
    void 
    Revolute::DrawPart<1>()
    {
      const double scale = Core::System::Get().GetSettings().GetScalePart()/100.;
      //glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
      Core::Drawable::DrawCylinder(0.01f*scale, 1.0f*scale);

      glRotatef(90.0f, 0.0f, 0.0f, 1.0f);
      glTranslatef(0.0f, 0.35f*scale, 0.0f);
      Core::Drawable::DrawCylinder(0.01f*scale, 0.1f*scale);
      glTranslatef(0.0f, -2*0.35f, 0.0f);
      Core::Drawable::DrawCylinder(0.01f*scale, 0.1f*scale);
    }

    

    template<>
    void 
    Revolute::BuildPoints<1>(Core::SetOf<PartUserPoint>& set, Core::Drawable& item)
    {
      assert(set.GetTotItemNumber()==0);
      PartUserPoint* p1 = new PartUserPoint(item);
      PartUserPoint* p2 = new PartUserPoint(item);
      PartUserPoint* p3 = new PartUserPoint(item);
      p1->GetName() = "left";
      p1->SetScaleCoordinate(-0.5, 0., 0.);
      p2->GetName() = "middle";
      p2->SetScaleCoordinate( 0., 0., 0.);
      p3->GetName() = "right";
      p3->SetScaleCoordinate( 0.5, 0., 0.);
      set.AddItem(*p1);
      set.AddItem(*p2);
      set.AddItem(*p3);
    }


    template<> 
    void 
    Revolute::DrawPart<2>()
    {
      const double scale = Core::System::Get().GetSettings().GetScalePart()/100.;
      //glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
      Core::Drawable::DrawCylinder(0.1f*scale, 0.6f*scale);
    }


    template<>
    void 
    Revolute::BuildPoints<2>(Core::SetOf<PartUserPoint>& set, Core::Drawable& item)
    {
      assert(set.GetTotItemNumber()==0);
      PartUserPoint* p1 = new PartUserPoint(item);
      p1->GetName() = "middle";
      p1->SetScaleCoordinate(0., 0., 0.);
      set.AddItem(*p1);
    }



    void 
    Revolute::DrawIcon(QIcon& icon, QColor color1, QColor color2)
    {
      const int size = Gui::MainWindow::Get().GetIconSize();
      const int margin = 2;
      QPixmap pixmap(QSize(size,size));
      pixmap.fill(QColor (0, 0, 0, 0));
      QPainter painter(&pixmap);
      painter.setPen(Qt::black);

      const int thickness = size/4;
      const int middle = size/2;
      
      QLinearGradient gradient1(middle, middle-(thickness/2), middle, middle+(thickness/2));
      gradient1.setColorAt(0.25, color1);
      gradient1.setColorAt(0.5, Qt::white);
      gradient1.setColorAt(0.75, color1);
      QBrush brush1(gradient1);
      painter.setBrush(brush1);
      painter.drawRect(margin, middle-thickness/2, size-2*margin, thickness);
      

      QLinearGradient gradient2(middle, middle-(thickness/2)-thickness, middle, middle-(thickness/2));
      gradient2.setColorAt(0.25, color2);
      gradient2.setColorAt(0.5, Qt::white);
      gradient2.setColorAt(0.75, color2);
      QBrush brush2(gradient2);
      painter.setBrush(brush2);
      painter.drawRect(margin*2, middle-(3*thickness/2), size-4*margin, thickness);

      QLinearGradient gradient3(middle, middle+(thickness/2), middle, middle+(thickness/2)+thickness);
      gradient3.setColorAt(0.25, color2);
      gradient3.setColorAt(0.5, Qt::white);
      gradient3.setColorAt(0.75, color2);
      QBrush brush3(gradient3);
      painter.setBrush(brush3);
      painter.drawRect(margin*2, middle+(thickness/2), size-4*margin, thickness);

      icon = QIcon(pixmap);
    }
    
  


  }
}




